package com.ddm.controller;

import com.ddm.service.IProjectService;
import com.dto.AjaxResult;
import com.entity.Project;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by he on 2025/3/4 13:27
 */
@RequestMapping("/admin/project")
@RestController
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    // 根据项目ID查询
    @GetMapping("/{projectId}")
    public AjaxResult getProjectById(@PathVariable("projectId") Integer projectId) {
        Project project = projectService.selectProjectById(projectId);
        return project != null ? AjaxResult.success(project) : AjaxResult.error("项目未找到");
    }

    // 新增项目
    @PostMapping
    public AjaxResult addProject(@RequestBody Project project) {
        int rows = projectService.insertProject(project);
        return rows > 0 ? AjaxResult.success("新增项目成功") : AjaxResult.error("新增项目失败");
    }

    // 修改项目信息
    @PutMapping
    public AjaxResult updateProject(@RequestBody Project project) {
        int rows = projectService.updateProject(project);
        return rows > 0 ? AjaxResult.success("修改项目信息成功") : AjaxResult.error("修改项目信息失败");
    }

    // 根据项目ID删除
    @DeleteMapping("/{projectId}")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public AjaxResult deleteProjectById(@PathVariable Integer projectId) {
        int rows = projectService.deleteProjectById(projectId);
        return rows > 0 ? AjaxResult.success("删除项目成功") : AjaxResult.error("删除项目失败");
    }

    // 批量删除项目
    @DeleteMapping("/batch/{projectIds}")
    public AjaxResult deleteProjectBatch(@PathVariable Integer[] projectIds) {
        int rows = projectService.deleteProjectByIds(projectIds);
        return rows > 0 ? AjaxResult.success("批量删除项目成功") : AjaxResult.error("批量删除项目失败");
    }

    // 分页查询项目
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Project project) {
        List<Project> list = projectService.selectProjectList(project);
        PageInfo<Project> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }
}